package com.model.dao;

import java.util.List;

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

@Stateless
public class MasterCatalogTableDAO {

	private static final Log log = LogFactory
			.getLog(MasterCatalogTableDAO.class);

	Session session;
	Transaction tx;

	public void persist(MastercatalogTable transientInstance) throws Exception {
		log.debug("persisting MastercatalogTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.persist(transientInstance);
			tx.commit();
			log.debug("persist successful");
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
			throw ex;
		}
	}

	public void remove(MastercatalogTable persistentInstance) throws Exception {
		log.debug("removing MastercatalogTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			log.debug("remove successful");
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("remove failed", ex);
			throw ex;
		}
	}

	public MastercatalogTable merge(MastercatalogTable detachedInstance) throws Exception {
		log.debug("merging MastercatalogTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			MastercatalogTable result = (MastercatalogTable) session
					.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("merge failed", ex);
			throw ex;
		}
	}

	public MastercatalogTable findById(int id) {
		log.debug("getting MastercatalogTable instance with id: " + id);
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			MastercatalogTable instance = (MastercatalogTable) session.get(
					MastercatalogTable.class, id);
			tx.commit();
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<MastercatalogTable> findAll(){
		log.debug("getting All MastercatalogTable instances ");
		try {
			Session session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			Transaction tx = session.beginTransaction();
			Query query = session.createQuery("from MastercatalogTable");
			List<MastercatalogTable> list = query.list();
			tx.commit();
			log.debug("get successful");
			return list;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<MastercatalogTable> findByKeyword(String keyword) {
		log.debug("getting All MastercatalogTable instances ");
		try {
			Session session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			Transaction tx = session.beginTransaction();
			String var = "%" + keyword + "%";
			Query query = session
			.createQuery("from MastercatalogTable where itemName like '"
					+ var + "' or itemBrand like '" + var
					+ "' or itemDescription like '" + var+"'");
			List<MastercatalogTable> list = query.list();
			tx.commit();
			log.debug("get successful");
			return list;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List<MastercatalogTable> findByMenu(String level,Integer menuId) {
		log.debug("getting All MastercatalogTable instances ");
		try {
			Session session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			Transaction tx = session.beginTransaction();
			Query query = session
			.createQuery("from MastercatalogTable where "+level+"="+menuId);
			List<MastercatalogTable> list = query.list();
			tx.commit();
			log.debug("get successful");
			return list;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public static void main(String[] args) {
		try {
			Session session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			Transaction tx = session.beginTransaction();
			String level="l2_id";
			Integer menuId=3;
			Query query = session
			.createQuery("from MastercatalogTable where "+level+"="+menuId);
			List<MastercatalogTable> list = query.list();
			tx.commit();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			log.error("get failed", ex);
//			throw ex;
		}
	}
}
